'use strict';

angular.module('app')
    .controller('mediaController', ['$rootScope', '$scope', '$http', '$state','$timeout',
        function($rootScope, $scope, $http, $state, $timeout) {
            $scope.title = '媒体大全列表';
            $scope.param = { };
            $scope.loading = false;

            $scope.search = function (pageNum) {
                if(pageNum==1){
                    $scope.param.pageNum = 1;
                }
                $scope.param.type = 1;//只显示友情链接
                $scope.loading = true;
                $.ajax({
                    url : $scope.app.project_name + '/media/list',
                    data: $scope.param
                }).then(function(result) {
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.pageInfo = result.data;
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });
            }

            $scope.fileDown = function (name) {
                window.location.href = $scope.app.project_name + "/templet/download?fileName=" + name;
            }

            $scope.oper = function (id,$event) {
                $.ajax({
                    url : $scope.app.project_name + '/media/disable',
                    data: {'id': id, 'enable' : $event.target.checked?'Y':'N'}
                }).then(function(result) {
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.search();
                    } else {
                        $scope.msg = result.msg;
                    }
                });
            }

            //批量删除
            $scope.batchDel=function () {
                if($scope.selected.length<=0){
                    $scope.alertModal({"content":"请选择要刪除的项"});
                    return;
                }
                $scope.alertMessage({
                    text:"确定做此操作吗？", //内容
                    btnCancel:true, //取消按钮
                    btnConfirm:true //确认按钮
                }).then(function () {
                    $.ajax({
                        url : $scope.app.project_name + '/media/batchDel',
                        data: {'ids':$scope.selected}
                    }).then(function(result) {
                        $scope.loading = false;
                        if (result.httpCode == 200) {
                            $scope.selected = [];
                            $scope.search();
                            $scope.alertModal({"content":"刪除成功"});
                        } else {
                            $scope.alertModal({"content":"刪除失败"});
                        }
                    });
                })
            }

            $scope.search();
            //重置按钮的操作
            $scope.clearSearch = function() {
                $scope.param.keyword= null;
                $scope.param={};//将输入的数据清空
                $scope.search();
            }

            $scope.selected = [];
            var updateSelected = function (action, contact) {
                if (action == 'add' && $scope.selected.indexOf(contact.id) == -1){
                    $scope.selected.push(contact.id);
                    contact.checked = true;
                }

                if (action == 'remove' && $scope.selected.indexOf(contact.id) != -1){
                    $scope.selected.splice($scope.selected.indexOf(contact.id), 1);
                    contact.checked = false;
                }
            };

            $scope.updateSelection = function ($event, contact) {
                var checkbox = $event.target;
                var action = (checkbox.checked ? 'add' : 'remove');
                if (action == 'add' && $scope.selected.indexOf(contact.id) == -1){
                    $scope.selected.push(contact.id);
                    contact.checked = true;
                }
                if (action == 'remove' && $scope.selected.indexOf(contact.id) != -1){
                    $scope.selected.splice($scope.selected.indexOf(contact.id), 1);
                    contact.checked = false;
                }
            };
            // 全选操作
            $scope.selectAll = function ($event) {
                var checkbox = $event.target;
                var action = (checkbox.checked ? 'add' : 'remove');
                for (var i = 0; i < $scope.pageInfo.records.length; i++) {
                    var contact = $scope.pageInfo.records[i];
                    updateSelected(action, contact);
                }
            };

            $scope.isSelected = function (id) {
                return $scope.selected.indexOf(id) >= 0;
            };
            // 翻页
            $scope.pagination = function (page) {
                $scope.param.pageNum=page;
                $scope.search();
            };

            //获取选中的文件名
            $scope.showInfo=function(obj){

                var a=document.getElementById("file").files[0];

                // var filename = "文件名：" + obj.value.substring(pos+1);
                document.getElementById("fileName").innerText ="导入文件："+a.name;
            }

            //导入EXCEL
            $scope.uploadFile = function (){
                var fileObj = document.getElementById("file").files[0]; // 获取文件对象
                if(fileObj){
                    console.log(fileObj);
                    // FormData 对象
                    var form = new FormData();
                    form.append("file", fileObj);// 文件对象
                    $.ajax({
                        url :$scope.app.project_name+'/media/batchAdd',
                        type: 'POST',
                        data: form,
                        async: false,
                        cache: false,
                        contentType: false,
                        processData: false
                    }).then(function(result) {
                        console.log(result);
                        $scope.loading = false;
                        if (result.httpCode == 200) {
                            var msg = result.errorMsg;
                            if (msg != '' && msg != null) {
                                document.getElementById("importResult").innerText="导入结果 : 失败！"+msg;
                            } else {
                                document.getElementById("importResult").innerText = "导入结果 : 成功";
                            }
                        }else{
                            $scope.alertModal({"content":result.msg});
                        }
                     $scope.$apply();
                    });
                }else{
                    $scope.alertModal({"content":"未选择文件"});
                }
            }

        } ]);